import * as Types from '../constants'
import {
	login,
	authLogin
} from 'network/login.js'

import {
	getUserData,
	getUserDesc
} from 'network/user.js'

export default {
	// 登录
	[Types.loginAction](context, user) {
		return new Promise(async (resolve, reject) => {
			const res = await login(user)
			if (res.meta.code === 200) {
				context.commit(Types.login, res.data)
				resolve(res.data)
			} else {
				context.commit(Types.logout)
				reject(res.meta.message)
			}
		})
	},

	// token 登录
	[Types.authLoginAction](context) {
		return new Promise(async (resolve, reject) => {
			const res = await authLogin()
			if (res.meta.code === 200) {
				context.commit(Types.login, res.data)
				resolve(res.data)
			} else {
				context.commit(Types.logout)
				reject(res.meta.message)
			}
		})
	},

	// 退出登录
	[Types.logoutAction](context) {
		return new Promise((resolve, reject) => {
			context.commit(Types.logout)
			resolve()
		})
	},

	// 获取用户数据
	[Types.getUserDataAction](context, userId) {
		return new Promise(async (resolve, reject) => {
			const res = await getUserData(userId)
			if (res.meta.code === 200) {
				context.commit(Types.getUserData, res.data)
				resolve(res.data)
			} else {
				reject(res.meta.message)
			}
		})
	},

	// 获取用户详情
	[Types.getUserDescAction](context, userId) {
		return new Promise(async (resolve, reject) => {
			const res = await getUserDesc(userId)
			if (res.meta.code === 200) {
				context.commit(Types.getUserDesc, res.data)
				resolve(res.data)
			} else {
				reject(res.meta.message)
			}
		})
	}
}
